var CardSendParam = {
	'dataType':'json',
	'beforeSubmit':toutiaoBefore,
	'success':toutiaoResponse,
	'error':toutiaoError,
};

var CouponSendParam = {
    'dataType':'json',
    'beforeSubmit':CouponBefore,
    'success':CouponResponse,
    'error':toutiaoError,
};
function toutiaoBefore(formData, jqForm, options){
    for (var i = 0; i < formData.length; i++) {
        var t = $('input[name="'+formData[i].name+'"]').attr('alt');
        if(formData[i].value==''){
            if(formData[i].type=='file'){
                toastr.error('请上传'+t+'！');
                return false;
            }else{
                var nameV = formData[i].name;
                if(nameV!='limitday'&&nameV!='num_198'&&nameV!='num_258'&&nameV!='num_298'){
                    toastr.error('请填写'+t+'！');
                    return false;
                }
                if(nameV=='limitday'&&$('input[name="business_type"]:checked').val()=='XS'){
                    toastr.error('请填写'+t+'！');
                    return false;
                }
                if($('input[name="business_type"]:checked').val()=='ZH'&&nameV=='num_198'&&nameV=='num_258'&&nameV=='num_298'){
                    toastr.error('请填写'+t+'！');
                    return false;
                }
            }
        }
        if(!$('input[name="business_type"]:checked').val()){
            toastr.error('请选择卡的类型！');
            return false;
        }
    }
}

function CouponBefore(formData, jqForm, options){
    for (var i = 0; i < formData.length; i++) {
        var t = $('input[name="'+formData[i].name+'"]').attr('alt');
        if(formData[i].value==''){
            if(formData[i].type=='file'){
                toastr.error('请上传'+t+'！');
                return false;
            }else{
                var nameV = formData[i].name;
                if(nameV!='limitday'&&nameV!='num_198'&&nameV!='num_258'&&nameV!='num_298'){
                    toastr.error('请填写'+t+'！');
                    return false;
                }
                if(nameV=='limitday'&&$('input[name="business_type"]:checked').val()=='XS'){
                    toastr.error('请填写'+t+'！');
                    return false;
                }
                if($('input[name="business_type"]:checked').val()=='ZH'&&nameV=='num_198'&&nameV=='num_258'&&nameV=='num_298'){
                    toastr.error('请填写'+t+'！');
                    return false;
                }
            }
        }
    }
}

function toutiaoResponse(response) {
	if(response.success){
        setTimeout(function(){
            location.href = '/manageCard';
        },2000);
		toastr.success(response.message);
	}else{
		toastr.error('修改失败，请联系开发人员！');
	}
}

function CouponResponse(response) {
    if(response.success){
        setTimeout(function(){
            location.href = '/manageCoupon';
        },2000);
        toastr.success(response.message);
    }else{
        toastr.error('修改失败，请联系开发人员！');
    }
}

function toutiaoError(XMLHttpRequest, textStatus, errorThrown) {
	if(XMLHttpRequest.status==422){
		var RegError = XMLHttpRequest.responseJSON.errors;
		for(var o in RegError){
			for (var i = 0; i < RegError[o].length; i++) {
				toastr.error(RegError[o][i]);
			}
		}
	}
}
// addCouponForm
ajaxForm_('#addCardForm', CardSendParam);
ajaxForm_('#addCouponForm', CouponSendParam);

document.onkeydown = function(event) {
    var target, code, tag;  
    if (!event) {  
        event = window.event; //针对ie浏览器
        target = event.srcElement;  
        code = event.keyCode;  
        if (code == 13) {  
            tag = target.tagName;  
            if (tag == "TEXTAREA") { return true; }
            else { return false; }  
        }  
    }  
    else {  
        target = event.target; //针对遵循w3c标准的浏览器，如Firefox
        code = event.keyCode;
        if (code == 13) {
            tag = target.tagName;
            if (tag == "INPUT") { return false; }
            else { return true; }
        }  
    }  
};